package com.terren.spider.core.html.scheme.impl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import com.terren.spider.entity.core.Entry;
import com.terren.spider.util.common.StringUtil;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.utils.UrlUtils;

public class CentanetErshoufangScheme extends BasicCustomScheme{


	@Override
	public Set<String> findHelpPaginationUrl(Page page, Entry entry) {
		Set<String> resultSet = new HashSet<>();
		resultSet.addAll(findPagination(page, entry)); // 分页部分
		return resultSet;
	}
	
	/**
	 * 分页
	 * @param page
	 * @param entry
	 * @return
	 */
	private Set<String> findPagination(Page page, Entry entry) {
		List<String> pgList = new ArrayList<String>();// 存放分页list
		String pageUrl = "";
		String lastUrl = "";
		List<String> pageUrls = page.getHtml().$("body > div.section-wrap.section-pager > div > div > div").links().all();//判断是否有分页，获取当前页面所有分页链接
		List<String> lastUrls = page.getHtml().$("body > div.section-wrap.section-pager > div > div > div > a:nth-child(7)").links().all();//获取最好页的url
		if (pageUrls != null && pageUrls.size() >= 2) {
			pageUrl = pageUrls.get(1);
		}else{
			pgList.addAll(pageUrls);
		}
		if(lastUrls != null && lastUrls.size() !=0 ){
			lastUrl = lastUrls.get(0);
		}
		if (!"".equals(pageUrl) && !"".equals(lastUrl)) {
			String pageNum = StringUtil.getNumbers(lastUrl); 	
			Integer totalPage = Integer.parseInt(pageNum.trim());
			String domain = UrlUtils.getDomain(entry.getEntryUrl());
			for (int i = 2; i <= totalPage; i++) {
				pgList.add("https://"+domain + "/ershoufang/g"+i+"/");
			}
		}
		Set<String> resultSet = new HashSet<>();
		resultSet.addAll(pgList);
		resultSet.remove(null);
		return resultSet;
	}

}
